-- Game
-- 游戏绘制区
Game = class()

function Game:init()
    
    self.mode = Mode.earth -- 重力模式
    self.world = World()
    self.clouds = drawCloud()
    if self.mode ~= Mode.space then
        self.world:addGround() -- 添加地面
    end
    parameter.action("PhysicsPause", self.world.pause)
    parameter.action("PhysicsResume", self.world.rusume)
    physics.continuous = true
    smooth()
    self.particleList = ParticleList()
    self.move = Move(self.world)
    self.gui = GuiRenderer()
    self.fileName = ""
    physics.gravity(0, -self.mode.gravity * 100)
end

function Game:newGame(fileName)
    self.world:clear()
    self.fileName = fileName
    GameState.State = Gaming
end

function Game:readGame(fileName)
   -- music.stop()
    self.fileName = fileName
    self.world:read(fileName)
    GameState.State = Gaming
end

function Game:saveGame()
    self.world:save(self:getFileName())
end

function Game:getFileName()
    return self.fileName
end

function Game:draw(a)
    if self.mode == Mode.space then
        background(27, 29, 53, 255)
    else
        
        background(Colour.SkyBlue)
        self. clouds:update()
        self. clouds:draw()
    end
    self.particleList:draw(a)
    self.world:draw()
    self.gui:draw()
end

function Game:touched(touch)
    if self.gui:touched(touch) then return end
    local object = self.gui.bar:getCurrentItem()
    if object and object.objectType == ObjectCreater.Type.AdvancedObject then
        object:createTouch(touch)
    else
        if self:processMoveTouch(touch) then return end
        if touch.state == ENDED then
            if touch.tapCount == 3 then
                self.world:explode(vec2(touch.x,touch.y),10000,10000)
                sound(SOUND_EXPLODE, 16417)
            else
                -- 添加物品
                if object then
                    self.world:addObject(object:createBody(touch.x,touch.y))
                end
            end
        end
    end
end

function Game:processMoveTouch(touch)
    if self.move:touched(touch) then return true end
    return false
end

function Game:getLastInsert()
    self.world:removeObject(#world.objects)
end

function Game:tick()
    self.world:tick()
    self.particleList:tick()
end
